#include "StdAfx.h"
#include <string>

namespace quiz
{

//===========================================================================
// Simple Hash Table
//===========================================================================
template< typename T >
class SimpleHashTable
{

public :
    
    SimpleHash(
        unsigned int size
    )   : m_size( size )
    {
        this->m_data = this->m_size > 0
                     ? new T[ this->m_size ]
                     : NULL
    }

    virtual ~SimpleHash() {
        this->CleanUp();
    }

    bool Set(
        const char * key,
        const T &    value
    ) {
        if( !key || !*key )
            return false;
        
        unsigned int index = this->Hash( key );
        m_pData[ index ] = value;

        return true;
    }

    T get(
        const char * key
    ) {
        unsigned int index = this->Hash( key );
        return this->m_data[ index ];
    }

private :
    
    T *          m_data;
    unsigned int m_size;

    unsigned int Hash(
        cosnt char * key
    ) {
        unsigned int hash = 5381;

        for( unsigned int i = 0; i < strlen( key ); i++ )
            hash = ( ( hash << 5 ) + hash )
                 + static_cast< unsigned int >( key[ i ] );

        return hash % this->m_size;
    }    

    void CleanUp( void ) {
        if( this->m_data ) {
            delete [] this->m_data;
            this->m_data = NULL;
            this->m_size = 0;
        }
    }
};

};